package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyContent;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("daily")
public class DailyController {


    @Autowired
    private IDailyService dailyService;
    // 待审核游记列表
    @RequestMapping("list")
    public String list(Model model , @ModelAttribute("qo") DailyQueryObject qo){
        qo.setStatus(Daily.STATUS_WAITING);
        qo.setOrderBy("d.createTime desc");
        PageInfo result = dailyService.query(qo);
        model.addAttribute("result",result);
        return "daily/list";
    }
    // 拒绝发布游记列表
    @RequestMapping("refuseList")
    public String refuseList(Model model , @ModelAttribute("qo") DailyQueryObject qo){
        //修改状态为拒绝发布的状态
        qo.setStatus(Daily.STATUS_FORBID);
        //按照创建时间来排序
        qo.setOrderBy("d.createTime desc");
        PageInfo result = dailyService.query(qo);
        model.addAttribute("result",result);
        return "daily/refuseList";
    }
    // 已发布游记列表
    @RequestMapping("releaseList")
    public String releaseList(Model model , @ModelAttribute("qo") DailyQueryObject qo){
        //修改状态为拒绝发布的状态
        qo.setStatus(Daily.STATUS_RELEASE);
        //按照创建时间来排序
        qo.setOrderBy("d.createTime desc");
        PageInfo result = dailyService.query(qo);
        model.addAttribute("result",result);
        return "daily/releaseList";
    }

    // 改变游记状态
    @RequestMapping("changeStatus")
    @ResponseBody
    public JSONResult changeState(Daily daily){
        JSONResult result = new JSONResult();
        try {
            dailyService.changeStatus(daily);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }


    @RequestMapping("getDailyContentById")
    @ResponseBody
    public DailyContent getDailyContentById(Long dailyId){
        return  dailyService.getDailyContentById(dailyId);
    }
}
